import Vue from 'vue';
import Vuex from 'vuex';
import qs from 'qs';
import * as api from "../api";

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        init: {},
        tag: '',
        tableData: [],
        detail: {},
        total: 0,
        isSidebar: true,
        common: null,
        path: '',
        codeTypes: {},
        menu: sessionStorage.getItem('menu') ? qs.parse(sessionStorage.getItem('menu')) : [],
        steps: 0,
        disabled: false,
        store: {},
        selIndex: -1,
        checked: [],
        dbClick: false,
        column: '',
        personid: {},
        companyid: {},
        backFun: null,
        cacheData: {},
        close: false,
        rowIndex: -1,
        row:{}
    },
    actions: {
        // 更新初始
        SET_INIT({commit}, value) {
            
            let query = {};
            if (Vue.prototype.route.query.ticket !== undefined && Vue.prototype.route.query.projid !== undefined) { // 使用在对接天正
                query = {
                    ticket: Vue.prototype.route.query.ticket,
                    projid: Vue.prototype.route.query.projid
                }
                console.log('abc',query)
            }
            
            let params = {
                ...value,
                ...query
            };
            api.commonPost(store.getters.path + '/doInit', params).then(res => {
                commit('SET_BACKFUN', res.backFunType);
                if (res.status === '0') {
                    let mdParam = {
                        mdParam: res.data.mdParam || {}
                    };
                    sessionStorage.setItem(store.getters.path + '/mdParam', qs.stringify(mdParam));

                    commit('SET_COMMON', res.data);
                    commit('SET_CODETYPE', res.data);
                    commit('SET_INIT', res.data);
                }

                if (value.path.indexOf('doSave') != '-1' || value.path.indexOf('doAudit') != '-1') {
                    if (res.successMessage) {
                        Vue.prototype.$alert({
                            type: 'success',
                            title: res.successMessage
                        }).then(r => {
                        });
                    } else if (res.errorMessage && res.errorMessage.length > 0) {
                        Vue.prototype.$alert({
                            text: res.errorMessage,
                            textType: 'Array',
                            type: 'error',
                            title: '保存失败',
                            titleBody: '您提交的内容有如下问题：'
                        }).then(r => {
                            if (res.warnMessage && res.warnMessage.length > 0) {
                                Vue.prototype.$alert({
                                    text: res.warnMessage,
                                    textType: 'Array',
                                    title: '提示信息',
                                    type: 'warning'
                                })
                            }
                        });
                    } else if (res.warnMessage && res.warnMessage.length > 0) {
                        Vue.prototype.$alert({
                            text: res.warnMessage,
                            textType: 'Array',
                            type: 'warning',
                        }).then(r => {
                        });
                    }
                } else {
                    if (res.successMessage && res.successMessage.length > 0) {
                        res.successMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'success',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    } else if (res.errorMessage && res.errorMessage.length > 0) {
                        res.errorMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'error',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    } else if (res.warnMessage && res.warnMessage.length > 0) {
                        res.warnMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'warning',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    }
                }
            }).catch(() => {
                commit('SET_DISABLED', false);
            });
        },
        // 更新公共POST请求
        SET_COMMONPOST({commit}, value) {
            if (value.constructor !== Object) return;
            commit('SET_DISABLED', true);
            api.commonPost(value.path, value.params).then(res => {
                commit('SET_DISABLED', false);
                commit('SET_BACKFUN', res.backFunType);
                Vue.prototype.$hideLoading();
                if (res.status === '0') {
                    commit('SET_CODETYPE', res.data);
                    commit('SET_COMMON', res.data);
                }
                if (value.path.indexOf('doSave') != '-1' || value.path.indexOf('doAudit') != '-1') {
                    if (res.successMessage) {
                        Vue.prototype.$alert({
                            type: 'success',
                            title: res.successMessage
                        }).then(r => {

                        });
                    } else if (res.errorMessage && res.errorMessage.length > 0) {
                        Vue.prototype.$alert({
                            text: res.errorMessage,
                            textType: 'Array',
                            type: 'error',
                            title: '保存失败',
                            titleBody: '您提交的内容有如下问题：'
                        }).then(r => {
                            if (res.warnMessage && res.warnMessage.length > 0) {
                                Vue.prototype.$alert({
                                    text: res.warnMessage,
                                    textType: 'Array',
                                    title: '提示信息',
                                    type: 'warning'
                                })
                            }
                        });
                    } else if (res.warnMessage && res.warnMessage.length > 0) {
                        Vue.prototype.$alert({
                            text: res.warnMessage,
                            textType: 'Array',
                            type: 'warning',
                        }).then(r => {
                        });
                    }
                } else {
                    if (res.successMessage && res.successMessage.length > 0) {
                        res.successMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'success',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    } else if (res.errorMessage && res.errorMessage.length > 0) {
                        res.errorMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'error',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    } else if (res.warnMessage && res.warnMessage.length > 0) {
                        res.warnMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'warning',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    }
                }
            }).catch(() => {
                commit('SET_DISABLED', false);
                Vue.prototype.$hideLoading();
            })
        },
        // 更新公共GET请求
        SET_COMMONGET({commit}, value) {
            if (value.constructor !== Object) return;
            api.commonGet(value.path, value.params).then(res => {
                commit('SET_BACKFUN', res.backFunType);
                if (res.status === '0') {
                    commit('SET_CODETYPE', res.data);
                    commit('SET_COMMON', res.data);
                }
                if (value.path.indexOf('doSave') != '-1' || value.path.indexOf('doAudit') != '-1') {
                    if (res.successMessage) {
                        Vue.prototype.$alert({
                            type: 'success',
                            title: res.successMessage
                        }).then(r => {
                        });
                    } else if (res.errorMessage && res.errorMessage.length > 0) {
                        Vue.prototype.$alert({
                            text: res.errorMessage,
                            textType: 'Array',
                            type: 'error',
                            title: '保存失败',
                            titleBody: '您提交的内容有如下问题：'
                        }).then(r => {
                            if (res.warnMessage && res.warnMessage.length > 0) {
                                Vue.prototype.$alert({
                                    text: res.warnMessage,
                                    textType: 'Array',
                                    title: '提示信息',
                                    type: 'warning'
                                })
                            }
                        });
                    } else if (res.warnMessage && res.warnMessage.length > 0) {
                        Vue.prototype.$alert({
                            text: res.warnMessage,
                            textType: 'Array',
                            type: 'warning',
                        }).then(r => {
                        });
                    }
                } else {
                    if (res.successMessage && res.successMessage.length > 0) {
                        res.successMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'success',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    } else if (res.errorMessage && res.errorMessage.length > 0) {
                        res.errorMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'error',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    } else if (res.warnMessage && res.warnMessage.length > 0) {
                        res.warnMessage.map(item => {
                            Vue.prototype.$message({
                                type: 'warning',
                                message: item,
                                duration: 0,
                                showClose: true
                            })
                        })
                    }
                }
            }).catch(() => {
                commit('SET_DISABLED', false);
            });
        },
        // 更新左侧展开收缩
        SET_SIDEBAR({commit}, value) {
            commit('SET_SIDEBAR', value);
        },
        SET_PATH({commit}, value) {
            commit('SET_PATH', value);
        },
        SET_MENU({commit}, value) {
            commit('SET_MENU', value);
        },
        SET_STEPS({commit}, value) {
            commit('SET_STEPS', value);
        },
        SET_DISABLED({commit}, value) {
            commit('SET_DISABLED', value);
        },
        SET_STATE({commit}, value) {
            commit('SET_STATE', value);
        },
        SET_INDEX({commit}, value) {
            commit('SET_INDEX', value);
        },
        SET_CHECKED({commit}, value) {
            commit('SET_CHECKED', value);
        },
        SET_CLICK({commit}, value) {
            commit('SET_CLICK', value);
        },
        SET_COLUMN({commit}, value) {
            commit('SET_COLUMN', value);
        },
        SET_PERSONID({commit}, value) {
            commit('SET_PERSONID', value);
        },
        SET_COMPANYID({commit}, value) {
            commit('SET_COMPANYID', value);
        },
        SET_COMMON({commit}, value) {
            commit('SET_COMMON', value);
        },
        SET_CACHEDATA({commit}, value) {
            commit('SET_CACHEDATA', value);
        },
        SET_CLOSE({commit}, value) {
            commit('SET_CLOSE', value);
        },
        SET_CODETYPE({commit}, value) {
            commit('SET_CODETYPE', value);
        },
        SET_TAG({commit}, value) {
            commit('SET_TAG', value);
        },
        SET_ROWINDEX({commit}, value) {
            commit('SET_ROWINDEX', value);
        },
        SET_BACKFUN({commit}, value) {
            commit('SET_BACKFUN', value);
        }
    },
    getters: {
        init: state => state.init,
        tag: state => state.tag,
        tableData: state => state.tableData,
        detail: state => state.detail,
        total: state => state.total,
        isSidebar: state => state.isSidebar,
        common: state => state.common,
        path: state => state.path,
        codeTypes: state => state.codeTypes,
        menu: state => state.menu,
        steps: state => state.steps,
        disabled: state => state.disabled,
        state: state => state.state,
        backFun: state => state.backFun,
        selIndex: state => state.selIndex,
        checked: state => state.checked,
        dbClick: state => state.dbClick,
        column: state => state.column,
        personid: state => state.personid,
        companyid: state => state.companyid,
        loading: state => state.loading,
        cacheData: state => state.cacheData,
        close: state => state.close,
        rowIndex: state => state.rowIndex,
        row: state => state.row,
    },
    mutations: {
        // 设置初始
        SET_INIT(state, value) {
            state.init = value;
        },
        //切换显示业务标签
        SET_TAG(state, value) {
            state.tag = value;
        },
        // 设置分页总数
        SET_TOTAL(state, value) {
            state.total = value;
        },
        // 设置左侧展开收缩
        SET_SIDEBAR(state, value) {
            state.isSidebar = value;
        },
        // 设置公共的POST请求数据
        SET_COMMON(state, value) {
            state.common = value;
        },
        SET_PATH(state, value) {
            state.path = value;
        },
        SET_CODETYPE(state, value) {
            // if (value !== undefined && value !== null) {
            //     sessionStorage.setItem("codeTypes", qs.stringify(value));
            //     Object.keys(state.codeTypes).forEach(item => {
            //         let flag = false;
            //         let v = '';
            //         Object.keys(value).forEach(val => {
            //              if (item === val) {
            //                  flag = true;
            //                  v = val;
            //                  return
            //              }
            //         });
            //         if (flag) {
            //             state.codeTypes[item] = value[v];
            //         } else {
            //             if (value[v] !== undefined) {
            //                 state.codeTypes[v] = value[v];
            //             }
            //         }
            //     });
            // }
            if (value.codeTypes !== undefined && value.codeTypes !== null) {
                let codeTypes = {
                    ...value.codeTypes,
                    id: Math.random()
                };
                // console.log("vuex: ", codeTypes)
                sessionStorage.setItem(value.path, JSON.stringify(codeTypes));
                state.codeTypes = codeTypes;
            }
        },
        SET_MENU(state, value) {
            sessionStorage.setItem('menu', qs.stringify(value));
            state.menu = value;
        },
        SET_STEPS(state, value) {
            state.steps = value;
        },
        SET_DISABLED(state, value) {
            state.disabled = value;
        },
        SET_STATE(state, value) {
            state.state = value;
        },
        SET_BACKFUN(state, value) {
            state.backFun = value;
        },
        SET_INDEX(state, value) {
            state.selIndex = value;
        },
        SET_CHECKED(state, value) {
            state.checked = value;
        },
        SET_CLICK(state, value) {
            state.dbClick = value;
        },
        SET_COLUMN(state, value) {
            state.column = value;
        },
        SET_PERSONID(state, value) {
            state.personid = value;
        },
        SET_COMPANYID(state, value) {
            state.companyid = value;
        },
        SET_CACHEDATA(state, value) {
            if (!state.close) {
                state.cacheData[value.path] = value;
            }
            state.codeTypes = value.codeTypes;
        },
        SET_CLOSE(state, value) {
            state.close = value;
        },
        SET_ROWINDEX(state, value) {
            state.rowIndex = value;
        },
        SET_ROW(state, value) {
            state.row = value;
        }
    }
});

export default store
